package com.shengyuan.manage_os.old.ying.erpm;

import org.json.JSONArray;
import org.json.JSONException;

import com.shengyuan.manage_os.R;
import com.shengyuan.manage_os.old.ying.boce.task.GenericTask;
import com.shengyuan.manage_os.old.ying.boce.task.TaskAdapter;
import com.shengyuan.manage_os.old.ying.boce.task.TaskListener;
import com.shengyuan.manage_os.old.ying.boce.task.TaskParams;
import com.shengyuan.manage_os.old.ying.boce.task.TaskResult;
import com.shengyuan.manage_os.old.ying.erpm.controls.PictureButton;
import com.shengyuan.manage_os.old.ying.erpm.controls.SourceSelectMenu;
import com.shengyuan.manage_os.old.ying.erpm.controls.SourceSelectMenu.OnPopClickListener;
import com.shengyuan.manage_os.old.ying.erpm.tasks.Submit_System_Setting_Data_Task;
import com.shengyuan.manage_os.old.ying.erpm.util.Utility;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;

public class Actual_GC_CaiGouDan extends BaseActivity {

	protected EditText editText_baoxiaocontent,editText_jinqian, editText_baoxiaotype,editText_shenpiren,spinner_tasksource;
	protected PictureButton PictureButton_piaoju;
	protected PictureButton PictureButton_shiwu1, PictureButton_shiwu2, PictureButton_shiwu3, PictureButton_shiwu4;
	private String mID="0";
	
	public final static String  T_TASKSOURCE = "get_data_tasksource";
	private JSONArray mJsonArray;

	@Override
	public void onClick(View view) {
		super.onClick(view);
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate_copy(savedInstanceState);
		setContentView(R.layout.actual_gc_caigoudan);
		InitControls();
	}
    @Override
	public boolean onLongClick(View view) {
    	super.onLongClick(view);
		switch (view.getId()) {
		case R.id.spinner_tasksource:
			Utility.HideEditFouce(spinner_tasksource);

			Submit_System_Setting_Data_Task mSubmitTask = new Submit_System_Setting_Data_Task();
			mSubmitTask.setListener(mTaskListener);
			TaskParams params;
			params = new TaskParams();
			params.put(Submit_System_Setting_Data_Task.TYPENAME, this.T_TASKSOURCE);
			params.put(Submit_System_Setting_Data_Task.TYPECODE, LoginActivity.pmUser.UserName);
			mSubmitTask.execute(params);
			break;
		}
		return true;
	}
	@Override
	protected void SaveJsonData() {
		try {
			IsJson1 = true; mFinished = 1; mPrintenabled = 0;
			JsonMyID();
			getTask();
			getDataExt();
			
			getGPS();
			getCurrentUser();		
			getMemo();
			JsonPicture();
			save_pic();
		}catch (Exception e) {
		}
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (PictureButton_piaoju!=null) {
			PictureButton_piaoju.postInvalidate();
		}
		if (PictureButton_shiwu1!=null && PictureButton_shiwu1.IsExist()) {
			PictureButton_shiwu1.postInvalidate();
		}
		if (PictureButton_shiwu2!=null && PictureButton_shiwu2.IsExist()) {
			PictureButton_shiwu2.postInvalidate();
		}
		if (PictureButton_shiwu3!=null && PictureButton_shiwu3.IsExist()) {
			PictureButton_shiwu3.postInvalidate();
		}
		if (PictureButton_shiwu4!=null && PictureButton_shiwu4.IsExist()) {
			PictureButton_shiwu4.postInvalidate();
		}
	}
	protected void save_pic() throws JSONException {
		if (PictureButton_piaoju!=null) {
			mJson.put( PictureButton_piaoju.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_piaoju.mFileName);
		}
		if (PictureButton_shiwu1!=null && PictureButton_shiwu1.IsExist()) {
			mJson.put( PictureButton_shiwu1.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_shiwu1.mFileName);
		}
		if (PictureButton_shiwu2!=null && PictureButton_shiwu2.IsExist()) {
			mJson.put( PictureButton_shiwu2.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_shiwu2.mFileName);
		}
		if (PictureButton_shiwu3!=null && PictureButton_shiwu3.IsExist()) {
			mJson.put( PictureButton_shiwu3.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_shiwu3.mFileName);
		}
		if (PictureButton_shiwu4!=null && PictureButton_shiwu4.IsExist()) {
			mJson.put( PictureButton_shiwu4.FIELD+getString(R.string.title_actual_form_PictureButton), PictureButton_shiwu4.mFileName);
		}
	}
	
	@Override
	protected void SaveSelfPic(String sPath) {
		try {
			if (PictureButton_piaoju!=null) {
				CopyPicDest(PictureButton_piaoju, sPath);
			}
			if (PictureButton_shiwu1!=null && PictureButton_shiwu1.IsExist()) {
				CopyPicDest(PictureButton_shiwu1, sPath);
			}
			if (PictureButton_shiwu2!=null && PictureButton_shiwu2.IsExist()) {
				CopyPicDest(PictureButton_shiwu2, sPath);
			}
			if (PictureButton_shiwu3!=null && PictureButton_shiwu3.IsExist()) {
				CopyPicDest(PictureButton_shiwu3, sPath);
			}
			if (PictureButton_shiwu4!=null && PictureButton_shiwu4.IsExist()) {
				CopyPicDest(PictureButton_shiwu4, sPath);
			}	
		} catch (Exception e) {
			Utility.MessageBox(this,e.getMessage());
		}
	}

	protected void getDataExt() throws JSONException {
		if (editText_jinqian!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_jinqian), editText_jinqian.getText().toString());
		}		
		if (editText_baoxiaocontent!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_baoxiaocontent), editText_baoxiaocontent.getText().toString());
		}			
		if (editText_baoxiaotype!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_baoxiaotype), editText_baoxiaotype.getText().toString());
		}			
		if (editText_shenpiren!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_shenpiren), editText_shenpiren.getText().toString());
		}	
		if (spinner_tasksource!=null) {
			mJson.put( getString(R.string.title_actual_form_editText_tasksource), spinner_tasksource.getText().toString());
			mJson.put( getString(R.string.title_actual_form_editText_tasksource_id), mID);
		}	
	}
	
	@Override
	public void InitControls() {
		super.InitControls();
		editText_baoxiaocontent = (EditText) findViewById(R.id.editText_baoxiaocontent);
		editText_jinqian= (EditText) findViewById(R.id.editText_jinqian);
		editText_baoxiaotype= (EditText) findViewById(R.id.editText_baoxiaotype);
		editText_shenpiren= (EditText) findViewById(R.id.editText_shenpiren);
		spinner_tasksource = (EditText) findViewById(R.id.spinner_tasksource);

		spinner_tasksource.setOnLongClickListener(this);
		spinner_tasksource.setInputType(InputType.TYPE_NULL);
		
		PictureButton_piaoju = (PictureButton) findViewById(R.id.PictureButton_piaoju);
		PictureButton_shiwu1= (PictureButton) findViewById(R.id.PictureButton_shiwu1);
		PictureButton_shiwu2= (PictureButton) findViewById(R.id.PictureButton_shiwu2);
		PictureButton_shiwu3= (PictureButton) findViewById(R.id.PictureButton_shiwu3);
		PictureButton_shiwu4= (PictureButton) findViewById(R.id.PictureButton_shiwu4);
	}
	
	@Override
	public Boolean IsPassed() {
		if (super.IsPassed()) {
			if (!CheckEditEmpty(editText_baoxiaotype)) {
				return false;
			}
			if (!CheckEditNumber(editText_jinqian)) {
				return false;
			}
			if (!CheckEditEmpty(editText_baoxiaocontent)) {
				return false;
			}
			if (!CheckEditEmpty(editText_shenpiren)) {
				return false;
			}		
			if (!CheckEditEmpty(spinner_tasksource)) {
				return false;
			}
			if (PictureButton_piaoju!=null && !PictureButton_piaoju.IsExist()) {
				Utility.MessageBox( this,PictureButton_piaoju.FIELD );
				return false;
			}
		
			return true;
		}else {
			return false;
		}
	}

	
	private TaskListener mTaskListener = new TaskAdapter() {
		public ProgressDialog mDialog;
		@Override
		public void onPostExecute(GenericTask task, TaskResult result) {
			try {
			if (mDialog!=null) {
				mDialog.dismiss();
			}
			
				Submit_System_Setting_Data_Task t = (Submit_System_Setting_Data_Task)task;
				if (result == TaskResult.OK) {
					String data = t.pJsonArray.toString();
					if (data!=null && !data.equals("") && !data.equals("[]") ){
						mJsonArray = new JSONArray(data);
						String[] tasksource = new String[mJsonArray.length()];
						for (int i = 0; i < mJsonArray.length(); i++) {
							tasksource[i] = mJsonArray.getJSONObject(i).getString("TaskTitle");
						}
						if (tasksource!=null 
								&& tasksource.length>0) {
									SourceSelectMenu menu = new SourceSelectMenu( spinner_tasksource);
									menu.setArrays(tasksource);
									menu.show();
									menu.setOnPopClickListener(new onPopClickListener_tasksource());
								}
//						Utility.MessageBox(getString(R.string.setting_success_system_data));
					}
	
				} else {
				}
			} catch (Exception e) {
				// TODO: handle exception
			}

		}


		@Override
		public void onPreExecute(GenericTask task) {
			mDialog = Utility.ShowModelDialog( Actual_GC_CaiGouDan.this, getString(R.string.setting_system_update));
			mDialog.show();
		}
	};
	public class onPopClickListener_tasksource implements OnPopClickListener {
		@Override
		public void onPopClick(int index, String text, View anchor) {
			EditText etext = (EditText) anchor;
			if (etext!=null) {
				etext.setText(text);
			}
			try {
				mID = mJsonArray.getJSONObject(index).getString("ID");
			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

}
